package com.stadium.servlet.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stadium.bean.Equipment;
import com.stadium.repository.EquipmentRepository;
import com.stadium.servlet.EquipmentServlet;

@Service(value="EquipmentServlet")
public class EquipmentServletImpl implements EquipmentServlet {
	
	@Autowired
	private EquipmentRepository equipmentRepository;
	
	//返回所有为删除器材信息
	@Transactional(readOnly = true)
	public List<Equipment> getAll(){
		return equipmentRepository.getAllEquips();
	}
	
	//根据器材编号获取对应信息
	@Transactional(readOnly = true)
	public Equipment getById(Integer id){
		return equipmentRepository.findOne(id);
	}
	
	//器材分页信息
	@Transactional(readOnly = true)
	public Page<Equipment> getPages(int pageNum,int pageSize){
		Pageable page = new PageRequest(pageNum - 1, pageSize);
		return equipmentRepository.findAll(page);
	}

	//根据器材名字返回相应信息
	@Transactional(readOnly = true)
	public Equipment getByName(String name) {
		return equipmentRepository.getByName(name);
	}

	@Transactional
	public void save(Equipment equipment) {
		equipmentRepository.saveAndFlush(equipment);
	}

	@Transactional
	public void delete(Integer id) {
		equipmentRepository.delete(id);
	}
}
